package com.ujs.shc.controller;


import com.google.gson.Gson;
import com.ujs.shc.pojo.Comments;
import com.ujs.shc.pojo.User;
import com.ujs.shc.service.CommentsService;
import com.ujs.shc.service.UserService;
import com.ujs.shc.util.Msg;
import com.ujs.shc.util.UtilDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping(value = "/comments")
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    @Autowired
    private UserService userService;

    Gson gson = new Gson();

    /**
     * 评论(登录状态)
     * @param goodsId
     * @param content
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/doComm", method = {RequestMethod.POST}, produces = "text/plain;charset=utf-8")
    public String doComm(Integer goodsId, @RequestParam(value = "comment")String content, HttpServletRequest request){

        Msg msg = new Msg(0);
        if(goodsId == null){
            return gson.toJson(msg);
        }

        System.out.println(11);
        User loginUser = (User)request.getSession().getAttribute("currentUser");
        System.out.println(loginUser);
        if(loginUser== null){
            msg.setMessage("未登录");
            return gson.toJson(msg);
        }


        Comments comm = new Comments();
        comm.setUserId(loginUser.getId());
        comm.setGoodsId(goodsId);
        comm.setContent(content);
        comm.setCreateAt(UtilDate.getCurrentDateStrAll());
        System.out.println(comm);

        int rst = commentsService.addComments(comm);

        if(rst>0){
            msg.setCode(1);
        }
        return gson.toJson(msg);
    }

}
